$(function() {
	admin.cirlceBase.init();
});
Namespace('admin.cirlceBase', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	detail:function(id) {
		if (id != null) {
			top.addTab(id,'详情', contextPath+'/circle/base/detail?id=' + id);
		}
	},
	toUpdate : function(circleId) {
		getDialog('data-dialog').dialog({
			href:contextPath+'/circle/base/toUpdate/'+circleId,
			title: '修改圈子',
		    width: 500,
		    height: 450,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.cirlceBase.doUpdate(circleId);
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#data-dialog').dialog('close');
				}
			}]
		});
	},
	doUpdate : function() {
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		$('#data-dialog').dialog('close');
		
		$('#datagrid').datagrid('loading');
		var row = $('#datagrid').datagrid('getSelected');
		var circleId = data.id;
		$.post(contextPath+'/circle/base/doUpdate/'+circleId,data,function(result) {
			if(result.code==1) {
				top.showInfo('修改成功!');
				$('#datagrid').datagrid('reload');
			} else {
				top.showInfo('修改失败!'+result.msg);
				$('#datagrid').datagrid('reload');
			}
		});
	},
	toAdd : function() {
		top.addTab("1",'添加圈子', contextPath+'/circle/base/toAdd');
	},
	doAdd : function() {
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		if(data.logo==null || data.logo ==""){
			top.showInfo('请上传图片！');
			return false;
		}
		$('#data-dialog').dialog('close');
		$('#datagrid').datagrid('loading');
		$.post(contextPath+'/circle/base/doAdd',data,function(result) {
			if(result.code==1) {
				top.showInfo('创建成功!');
				$('#datagrid').datagrid('reload');
			} else {
				top.showInfo('创建失败!'+result.msg);
				$('#datagrid').datagrid('reload');
			}
		});
	},
	/**
	 * imgField: 图片存放字段
	 * showImgEle： 页面中图片显示element
	 */
	uploadLogo :function(imgField,showImgEle) {
		
		$('#filedata-form').form('submit',{
			onSubmit: function(){
				if ($('#uploadkey1').val()=='') {
					alert('请选择图片!');
					return false;
				}
				$('#uploadBtn').val('上传中...').attr('disabled',true);
			}, 
			success:function(result){
				$('#uploadBtn').val('上传').attr('disabled',false);
				var isjson = typeof(result) == "object" && Object.prototype.toString.call(result).toLowerCase() == "[object object]" && !result.length;
				if (!isjson) {
					result = JSON.parse(result);
				}
				if(result.code!=1){
					top.showInfo('上传失败,可能是文件太大（最大5m）!');
					return false;
				}
				var uploadResults=result.map.uploadResults;
				var imgHtml=[];
				var img=[];
				for(var i=0;i<uploadResults.length;i++){
					img.push(uploadResults[i].filePath);
					imgHtml.push('<img style="width:100px;height:100px;" src="http://qquan-hn-crazy.oss-cn-shenzhen.aliyuncs.com'+uploadResults[i].filePath+'">');
					imgHtml.push("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
				}
				$('#'+showImgEle).html(imgHtml.join(""));
				$('#'+imgField).val(img.join(";"));
			}
		});
	},
	/**
	 * 推荐圈子
	 */
	recommend: function (id) {
		var orderIndex = prompt("请输入推荐位置【1-99越小位置越靠前】:","1");
		if (orderIndex != null){
			var url = contextPath + '/circle/base/recommend';
			$('#datagrid').datagrid('loading');
			$.post(url, {'id': id,'recommendOidx':orderIndex}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('推荐成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
	                admin.cirlceBase.query();
				} else {
					top.showInfo(result.msg);
				}
			});
		}else{
			top.showInfo('请输入推荐位置');
		}
	},
	/**
	 * 取消推荐圈子
	 */
	unRecommend: function (id) {
        var url = contextPath + '/circle/base/unRecommend';
		$('#datagrid').datagrid('loading');
		$.post(url, {'id': id}, function (result) {
			$('#datagrid').datagrid('loaded');
			if (result.code == 1) {
				top.showInfo('操作成功');
				var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
                admin.cirlceBase.query();
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	/**
	 * 审核通过
	 */
	auditPass: function (id) {
        var url = contextPath + '/circle/base/auditPass';
		$('#datagrid').datagrid('loading');
		$.post(url, {'id': id}, function (result) {
			$('#datagrid').datagrid('loaded');
			if (result.code == 1) {
				top.showInfo('操作成功');
				var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
                admin.cirlceBase.query();
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	/**
	 * 审核不通过
	 */
	auditNotPass: function (id) {
        var url = contextPath + '/circle/base/auditNotPass';
		$('#datagrid').datagrid('loading');
		$.post(url, {'id': id}, function (result) {
			$('#datagrid').datagrid('loaded');
			if (result.code == 1) {
				top.showInfo('操作成功');
				var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
                admin.cirlceBase.query();
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	/**
	 * 取消审核通过--变为“待审批”
	 */
	undoAudit: function (id) {
        var url = contextPath + '/circle/base/undoAudit';
		$('#datagrid').datagrid('loading');
		$.post(url, {'id': id}, function (result) {
			$('#datagrid').datagrid('loaded');
			if (result.code == 1) {
				top.showInfo('操作成功');
				var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
                admin.cirlceBase.query();
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	/**
	 * 推荐圈子
	 */
	upLottery: function (id) {
        var url = contextPath + '/circle/base/upLottery';
		$('#datagrid').datagrid('loading');
		$.post(url, {'id': id}, function (result) {
			$('#datagrid').datagrid('loaded');
			if (result.code == 1) {
				top.showInfo('抽奖开始');
				var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
                admin.cirlceBase.query();
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	/**
	 * 推荐圈子
	 */
	downLottery: function (id) {
        var url = contextPath + '/circle/base/downLottery';
		$('#datagrid').datagrid('loading');
		$.post(url, {'id': id}, function (result) {
			$('#datagrid').datagrid('loaded');
			if (result.code == 1) {
				top.showInfo('抽奖结束');
				var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
                admin.cirlceBase.query();
			} else {
				top.showInfo(result.msg);
			}
		});
	}
});